package cn.xiaomubiao.oschine.adapter.tweets_pager_adapter;

import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.base.BaseRecyclerViewAdapter;
import cn.xiaomubiao.oschine.bean.Comment;
import cn.xiaomubiao.oschine.bean.Tweet;
import de.hdodenhof.circleimageview.CircleImageView;

import static android.media.CamcorderProfile.get;
import static cn.xiaomubiao.oschine.util.UIUtils.getContext;

/**
 * Created by hasee on 2017/4/5.
 */

public class TweetsDetailRecyclerAdapter extends BaseRecyclerViewAdapter {

    List<Comment> mComments;
    private MyOnClickListener mListener;

    public TweetsDetailRecyclerAdapter( List<Comment> list){
        mComments = list;

    }

    public interface MyOnClickListener{
        void onClick( View v, int position );
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder( ViewGroup parent, int viewType ) {
        View itemView = LayoutInflater.from(getContext()).inflate(R.layout.view_detail_pager_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position ) {
        Comment comment = mComments.get(position);
        final ViewHolder vH = (ViewHolder) holder;
        vH.mTvUsername.setText(comment.getAuthor());
        Spanned spanned = Html.fromHtml(comment.getContent());

        vH.mTvContent.setText(spanned);
//        vH.mTvContent.setMovementMethod(LinkMovementMethod.getInstance());
//        RichText.from(tweet.getBody())/*.urlClick(new OnUrlClickListener() {
//            @Override
//            public boolean urlClicked( String url ) {
//                ToastUtils.showToastShort(getContext(),url);
//                return true;
//            }
//        })*/.into(vH.mTvContent);
//        vH.mTvContent.setText(tweet.getBody());


        vH.mTvTime.setText(comment.getPubDate());
        if(comment.getPortrait().length() > 0) {
            Glide.with(getContext()).load(comment.getPortrait()).into(vH.mIvHeaderPhoto);
        } else {
            Glide.with(getContext()).load(R.drawable.widget_dface).into(vH.mIvHeaderPhoto);
        }

        vH.mLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                if(mListener != null) {
                    mListener.onClick(v,vH.getLayoutPosition()-1);
                }
            }
        });

    }

    public void setTweetsOnItemClickListener(MyOnClickListener l) {
        mListener = l;
    }


    @Override
    public int getItemCount() {
        return mComments != null ? mComments.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_header_photo)
        CircleImageView mIvHeaderPhoto;
        @BindView(R.id.tv_username)
        TextView mTvUsername;
        @BindView(R.id.tv_content)
        TextView mTvContent;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.ll_click)
        LinearLayout mLinearLayout;

        public ViewHolder( View itemView ) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }





    }



}
